import {
    $get,
    $post
} from '@/puigins/request.js';

export function getTemplates() {
    return new Promise((resolve, reject) => {
        $get('/devAssistant/getTemplates').then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function importDatasource(namespace, datasource, database, columns) {
    return new Promise((resolve, reject) => {
        $post('/devAssistant/importDatasource',
            {
                namespace, datasource, database, columns
            }).then(({ data }) => {
                resolve(data)
            }).catch(reject)
    })
}

export function addTemplate(ns_id, name) {
    return new Promise((resolve, reject) => {
        $post('/devAssistant/addTemplate', { ns_id, name }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}

export function addNamespace(name) {
    return new Promise((resolve, reject) => {
        $post('/devAssistant/addNamespace', { name }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}

export function saveTemplate(template) {
    return new Promise((resolve, reject) => {
        $post('/devAssistant/saveTemplate', template).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function getDataScource() {
    return new Promise((resolve, reject) => {
        $get('/devAssistant/getDataSource').then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
    let dataSource = []
    //i是数据源
    for (let i = 1, l = 4; i < l; i++) {
        let database = []
        //j是数据库
        for (let j = i % 2, n = 4; j < n; j++) {
            let tables = []
            //k是数据表
            for (let k = i * j * 10, m = i * j * 10 + 3; k < m; k++) {
                let columns = []
                //b是字段
                for (let b = k * 10, p = k * 10 + 3; b < p; b++) {
                    columns.push({
                        name: `字段${i}-${j}-${k}-${b}`,
                        sqlType: 'bigint',
                        javaType: 'java.lang.Long',
                        javaShortType: 'Long',
                        comment: '字段注释' + b * k,
                        table: '表' + k,
                        database: '数据库' + j,
                        datasource: '数据源' + i
                    })
                }
                tables.push({
                    name: '表' + k,
                    columns: columns,
                    database: '数据库' + j,
                    datasource: '数据源' + i
                })
            }
            database.push({
                name: '数据库' + j,
                children: tables,
                datasource: '数据源' + i
            })
        }
        dataSource.push({
            name: '数据源' + i,
            children: database
        })
    }
    return new Promise((resolve, reject) => {
        resolve(dataSource)
    })
}